﻿using System;
using System.IO;

namespace JoePack
{
    public class IOHelper
    {
        private string _rootPath;
        public IOHelper(string rootPath)
        {
            _rootPath = rootPath;
        }

        public string DependenciesPath
        {
            get { return Path.Combine(_rootPath, "dependencies.xml"); }
        }
        public string ReferenceFolderPath
        {
            get { return Path.Combine(_rootPath, "_References"); }
        }

        public void EnsureReferenceFolderExistOrCreateNew()
        {
            if (!Directory.Exists(this.ReferenceFolderPath))
            {
                Directory.CreateDirectory(this.ReferenceFolderPath);
            }
        }

        public void EnsurePackageFolderExistOrCreateNew(string packageName)
        {
            if (!Directory.Exists(this.DestinationPackageFolderPath(packageName)))
            {
                Directory.CreateDirectory(this.DestinationPackageFolderPath(packageName));
            }
        }

        public bool Sync(File file)
        {
            string sourceFilePath = file.SourcePath;
            string destinationFilePath = DestinationFilePath(file.Package.Name, file.FileName);

            if (!System.IO.File.Exists(sourceFilePath))
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Could not find source file:{0}. Check that file exist and is accesible!",Path.GetFullPath(sourceFilePath));
                Console.ResetColor();
                return false;
            }

            if (this.IsNewVersion(sourceFilePath, destinationFilePath))
            {
                try
                {
                    if (System.IO.File.Exists(destinationFilePath) && (System.IO.File.GetAttributes(destinationFilePath) == FileAttributes.ReadOnly))
                    {
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        Console.WriteLine(String.Format("Could not update file:{0} - checkout the file and re-run",Path.GetFullPath(destinationFilePath)));
                        Console.ResetColor();
                        return false;
                    }

                    System.IO.File.Copy(sourceFilePath, destinationFilePath, true);
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine("File {1} - updated from:{0}", sourceFilePath, destinationFilePath);
                    Console.ResetColor();
                    return true;
                }
                catch (Exception ex)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine(String.Format("Could not update file:{0} - details:{1}"), file.FileName, ex.ToString());
                    Console.ResetColor();
                }
            }
            return false;
        }

        private bool IsNewVersion(string sourceFilePath, string destinationFilePath)
        {
            if (!System.IO.File.Exists(destinationFilePath))
                return true;

            var sourceWriteDate = System.IO.File.GetLastWriteTime(sourceFilePath);
            var destinationWriteDate = System.IO.File.GetLastWriteTime(destinationFilePath);

            return sourceWriteDate > destinationWriteDate;
        }

        private string DestinationPackageFolderPath(string packageName)
        {
            return Path.Combine(ReferenceFolderPath, packageName);
        }

        private string DestinationFilePath(string packageName, string fileName)
        {
            return Path.Combine(DestinationPackageFolderPath(packageName), fileName);
        }
    }
}
